package leetcode.code322;

/**
 *
 * @author CaoYu
 * @date 2021年9月14日
 * @description
 */
public class Solution3 extends Solution322 {

	@Override
	public int coinChange(int[] coins, int amount) {
		if (amount == 0)
			return 0;
		int[] cache = new int[amount + 1];
		return this.coinChange(coins, amount, cache);
	}

	private int coinChange(int[] coins, int amount, int[] cache) {
		if (amount == 0) {
			return 0;
		}
		if (amount < 0) {
			return -1;
		}
		if (cache[amount] != 0) {
			return cache[amount];
		}
		int ans = Integer.MAX_VALUE;
		for (int coin : coins) {
			int res = coinChange(coins, amount - coin, cache);
			if (res != -1) {
				ans = Math.min(ans, res + 1);
			}
		}
		cache[amount] = ans == Integer.MAX_VALUE ? -1 : ans;
		return cache[amount];
	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
		so.debug6();
		so.debug7();
		so.debug8();
	}

}
